package com.ktgj.flight.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import com.ktgj.common.core.constant.FlightOrderConstants;
import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.flight.domain.FlightOrderInfoTickets;
import com.ktgj.flight.domain.FlightOrderIternaryDTO;
import com.ktgj.flight.domain.vo.FlightOrderIternaryVO;
import com.ktgj.flight.service.IFlightOrderInfoTicketsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.FlightOrderIternaryMapper;
import com.ktgj.flight.domain.FlightOrderIternary;
import com.ktgj.flight.service.IFlightOrderIternaryService;

/**
 * 机票订单行程单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-12
 */
@Service
public class FlightOrderIternaryServiceImpl implements IFlightOrderIternaryService 
{
    @Autowired
    private FlightOrderIternaryMapper flightOrderIternaryMapper;
    @Autowired
    private RemoteCustomerService remoteCustomerService;
    @Autowired
    private IFlightOrderInfoTicketsService flightOrderInfoTicketsService;

    /**
     * 查询机票订单行程单
     * 
     * @param invoiceId 机票订单行程单ID
     * @return 机票订单行程单
     */
    @Override
    public FlightOrderIternary selectFlightOrderIternaryById(Long invoiceId)
    {
        return flightOrderIternaryMapper.selectFlightOrderIternaryById(invoiceId);
    }

    /**
     * 查询机票订单行程单列表
     * 
     * @param flightOrderIternary 机票订单行程单
     * @return 机票订单行程单
     */
    @Override
    public List<FlightOrderIternary> selectFlightOrderIternaryList(FlightOrderIternary flightOrderIternary)
    {
        return flightOrderIternaryMapper.selectFlightOrderIternaryList(flightOrderIternary);
    }

    /**
     * 新增机票订单行程单
     * 
     * @param flightOrderIternary 机票订单行程单
     * @return 结果
     */
    @Override
    public int insertFlightOrderIternary(String token, FlightOrderIternary flightOrderIternary) throws Exception {
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        if (customer.getCode() == 200 && customer.getData() != null) {
            flightOrderIternary.setCreateTime(DateUtils.getNowDate());
            flightOrderIternary.setPayState(FlightOrderConstants.waitPay.getValue());
            flightOrderIternary.setPayCost(new BigDecimal(0.1));
            flightOrderIternary.setOrderState(1L);
            flightOrderIternary.setRefundStatus(0L);
            flightOrderIternary.setCustomerId(String.valueOf(customer.getData().getCustomerId()));
            return flightOrderIternaryMapper.insertFlightOrderIternary(flightOrderIternary);
        } else {
            throw new Exception(customer.getMsg());
        }
    }

    /**
     * 修改机票订单行程单
     * 
     * @param flightOrderIternary 机票订单行程单
     * @return 结果
     */
    @Override
    public int updateFlightOrderIternary(FlightOrderIternary flightOrderIternary)
    {
        return flightOrderIternaryMapper.updateFlightOrderIternary(flightOrderIternary);
    }

    /**
     * 批量删除机票订单行程单
     * 
     * @param invoiceIds 需要删除的机票订单行程单ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderIternaryByIds(Long[] invoiceIds)
    {
        return flightOrderIternaryMapper.deleteFlightOrderIternaryByIds(invoiceIds);
    }

    /**
     * 删除机票订单行程单信息
     * 
     * @param invoiceId 机票订单行程单ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderIternaryById(Long invoiceId)
    {
        return flightOrderIternaryMapper.deleteFlightOrderIternaryById(invoiceId);
    }

    //行程单列表
    @Override
    public List<FlightOrderIternaryDTO> orderIternaryList(FlightOrderIternaryVO iternary) {
        Calendar calendar = new GregorianCalendar();
        if (iternary.getDepartureTime() != null) {
            if (iternary.getDepartureTimeEnd() != null) {
                calendar.setTime(iternary.getDepartureTimeEnd());
                calendar.add(Calendar.DATE, 1);
                iternary.setDepartureTimeEnd(calendar.getTime());
            }
        }
        List<FlightOrderIternaryDTO> iternaryDTOS = flightOrderIternaryMapper.orderIternaryList(iternary);
        iternaryDTOS.forEach(item -> {
            List<FlightOrderInfoTickets> infoTicketsList = flightOrderInfoTicketsService.selectFlightOrderInfoTicketsByOrderId(Long.valueOf(item.getOrderId()));
            String name = "";
            for (FlightOrderInfoTickets orderInfoTickets : infoTicketsList) {
                name += orderInfoTickets.getGuestName() + " ";
            }
            item.setFlightPassengersName(name.substring(0, name.length() - 1));
        });
        return iternaryDTOS;
    }
}
